/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.controls
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	
	public class VolumeBar extends Sprite
	{
		private var bgLayer:Sprite;
		private var seekLayer:Sprite;
		private var controlLayer:Sprite;
		private var seekPoint:Sprite;
		private var seekPosition:int;
		private var size:Point;		
		private var mouseDown:Boolean;
		private var currentMouseX:Number = 0;
		
		public function VolumeBar(bgSize:Point, SeekPoint:Class)
		{
			super();
			
			bgLayer = new Sprite();
			bgLayer.mouseChildren = false;
			bgLayer.mouseEnabled = false;
			seekLayer = new Sprite();
			seekLayer.mouseChildren = false;
			seekLayer.mouseEnabled = false;
			seekPoint = SeekPoint();
			seekPoint.mouseChildren = false;
			seekPoint.mouseEnabled = false;	
			controlLayer = new Sprite();
			controlLayer.useHandCursor = true;
			controlLayer.buttonMode = true;
			controlLayer.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			controlLayer.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			controlLayer.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			controlLayer.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			
			addChild(bgLayer);
			addChild(seekLayer);
			addChild(seekPoint);
			addChild(controlLayer);
			
			setSize(bgSize);
		}
		private function mouseDownHandler(event:MouseEvent):void{
			mouseDown = true;
		}
		
		private function mouseMoveHandler(event:MouseEvent):void{
			if(event.buttonDown){
				if(mouseDown){					
					currentMouseX = mouseX;
					var proc:Number = mouseX/size.x*100;
				}
			}
		}
		
		private function mouseUpHandler(event:MouseEvent):void{
			mouseDown = false;
			currentMouseX = mouseX;
			var proc:Number = mouseX/size.x*100;
		}
		public function globalMouseOut():void{
			if(mouseDown){
				mouseDown = false;
				var proc:Number = (currentMouseX/size.x*100);
			}
		}
		private function mouseOutHandler(event:MouseEvent):void{
			//mouseDown = false;
		}
		
		public function setSize(bgSize:Point):void{
			if(!bgSize){
				return;
			}
			size = bgSize;
			draw();
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		public function set volume(value:Number):void{			
			if(value < 0){
				value = 0;
			}
			if(value > 100){
				value = 100;
			}
			if(value != seekPosition){
				seekPosition = value;
				
				draw();
			}
		}
		public function get volume():Number{
			return seekPosition;
		}	
		public function draw():void{
			
			if(size){
				var yOffset:Number = seekPoint.height/2 - size.y/2;
				
				controlLayer.graphics.clear();
				controlLayer.graphics.beginFill(0x000000,0.01);
				controlLayer.graphics.drawRect(0, 0, size.x, seekPoint.height);
				controlLayer.graphics.endFill();
				
				bgLayer.graphics.clear();
				bgLayer.graphics.beginFill(0x9E9E9E);
				bgLayer.graphics.drawRect(0, yOffset, size.x, size.y);
				bgLayer.graphics.endFill();
				
				seekLayer.graphics.clear();				
				if(seekPosition > 0){
					seekLayer.graphics.beginFill(0x0075BD);
					seekLayer.graphics.drawRect(0, yOffset, (size.x / 100 * seekPosition), size.y);
					seekLayer.graphics.endFill();
				}
				var seekX:Number = (size.x / 100 * seekPosition) - seekPoint.width/2;
				if(seekX < 0){
					seekX = 0;
				}
				if(seekX + seekPoint.width > size.x){
					seekX = size.x - seekPoint.width;
				}
				seekPoint.x = seekX;
				seekPoint.y = 0;
			}
		}
	}
}